package com.whut.whw.opersoncenter;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.util.AppBarStateChangeListener;
import com.whut.whw.util.GlideCircularLoadUtils;

import butterknife.BindView;

public class OPersonCenterActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;//工具栏

    @BindView(R.id.openson_role_img)
    ImageView openson_role_img;//用户头像

    @BindView(R.id.openson_center_bg)
    ImageView openson_center_bg;//背景

    @BindView(R.id.openson_center_sign)
    TextView openson_center_sign;//个性签名

    @BindView(R.id.collapsing_toolbar_layout)
    CollapsingToolbarLayout collapsing_toolbar_layout;

    @BindView(R.id.app_bar)
    AppBarLayout app_bar;

    @BindView(R.id.openson_center_name)
    TextView openson_center_name;//用户名

    @BindView(R.id.toolbar_title)
    TextView toolbar_title;

    @Override
    public int getLayoutId() {
        return R.layout.activity_operson_center;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initToolbar();
        initCenter();
    }

    /**
     * 初始化标题栏
     * 设置默认标题为空
     * 设置返回导航键
     */
    public void initToolbar() {
        toolbar.setTitle("");
        toolbar_title.setText("忘尘无憾");
        collapsing_toolbar_layout.setTitle("");
        setSupportActionBar(toolbar);//这玩意得有放置顺序，否则某些东西无法触发或相应
        toolbar.setNavigationIcon(R.drawable.rc_back_icon);
        setToolbarListener();

    }

    /**
     * 设置toolbar的监听事件
     */
    private void setToolbarListener() {
        //注：setSupportActionBar(toolbar)一定要放在setNavigationOnClickListener前,否则无法触发点击事件
        //返回按钮，销毁页面
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 初始化个人中心数据
     */
    public void initCenter() {

        Glide
                .with(context)
                .load("http://upload.jianshu.io/users/upload_avatars/3358298/167277b0-84f6-4b2c-a740-d117ab4687b6.jpg")
                .asBitmap().centerCrop()
                .into(new GlideCircularLoadUtils(context, openson_role_img));
        Glide
                .with(context)
                .load("https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG/skin/113.jpg")
                .asBitmap().centerCrop()
                .into(openson_center_bg);
        openson_center_sign.setText("天行健，君子以自强不息；地势坤，君子以厚德载物");
        openson_center_name.setText("忘尘无憾");
    }

    @Override
    public void setListener() {

        //CollapsingToolbarLayout展开、折叠监听
        app_bar.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.e("state", state.name());
                if (state == State.EXPANED) {//展开
                    toolbar_title.setVisibility(View.INVISIBLE);
                    openson_center_name.setVisibility(View.VISIBLE);
                } else if (state == State.COLLAPSED) {//折叠
                    toolbar_title.setVisibility(View.VISIBLE);
                    openson_center_name.setVisibility(View.INVISIBLE);
                } else {//中间状态
                    toolbar_title.setVisibility(View.INVISIBLE);
                    openson_center_name.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //设置菜单栏
        getMenuInflater().inflate(R.menu.menu_operson_center, menu);
        return super.onCreateOptionsMenu(menu);
    }
}
